export  default {
    /*
  * 调用微信支付接口
  * orderCodePass:支付的订单号
  * */
  getWxPay (resData,that) {
    let {orderCode,payInfo}=resData
    let pageThat=this
    return new Promise(resolve => {
      function onBridgeReady(){
        WeixinJSBridge.invoke(
          'getBrandWCPayRequest', {
            "appId":payInfo.appId,     //公众号名称，由商户传入
            "timeStamp":payInfo.payTimestamp,         //时间戳，自1970年以来的秒数
            "nonceStr":payInfo.payNonceStr, //随机串
            "package":payInfo.payPackage,
            "signType":payInfo.paySignType,         //微信签名方式：
            "paySign":payInfo.paySignature //微信签名
          },
          async function(res){
            console.log("支付回调了",res);
            if(res.err_msg == "get_brand_wcpay_request:ok" ){
              console.log("去查询订单状态");
              await pageThat.queryOrder(orderCode, that);
              console.log("去查询订单状态返回");
              resolve()
            }else{
              that.$toast.fail("支付失败");
            }
          });
      }
      if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
          document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
          document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
          document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
      }else{
        onBridgeReady();
      }
    })
  },
  circleTimer:0,
  // 根据订单号查询订单状态
  queryOrder (orderCode,that) {
    let currentThat=this;
    let toastExample=null;
    return new Promise(resolve => {
      function queryOrderSecond(orderCode,that){
        let data = {
          orderCode: orderCode
        }
        console.log("查询订单", orderCode);
        that.$khReqMethod({url:'/payscancode/h5/queryOrder?orderCode='+orderCode,method:"get"}).then((res) => {
          console.log('拉起支付的数据')
          console.log(res)
          if (res.data.code === 100) { // 支付成功
            that.$toast.success("支付成功");
            currentThat.sleep(1000).then(value => {
              resolve();
            })
          } else if (res.data.code === 102) { // 支付中
            toastExample=that.$toast.loading({
              message: '支付中...',
              forbidClick: true,
              loadingType: 'spinner'
            });
            if (currentThat.circleTimer < 2) {
              currentThat.circleTimer++
              currentThat.sleep(2000).then(value => {
                console.log("loading循环次数"+currentThat.circleTimer);
                queryOrderSecond(orderCode,that)//此处要注意
              })
            } else {
              currentThat.circleTimer = 0
              that.$toast('服务器处理异常');
            }
          } else if (res.data.code === 104) { // 支付失败
            that.$toast('支付失败,请重试');
          }
          // 隐藏支付中的loading框
          if (res.data.code === 100 || (res.data.code === 102 && currentThat.circleTimer === 20) || res.data.code === 104) {
            toastExample.clear()
          }
        })
      }
      queryOrderSecond(orderCode,that);  //要调用
    })
  },
  //防止按钮重复点击
  preventMulClick(that){
    return new Promise((resolve,reject) => {
      if (!that.$memberCardState.isCanClickBtn) {
        reject();
      }
      that.$memberCardState.isCanClickBtn = false
      resolve();
    })
  },
  //延时time时间后处理
  sleep (time) {
    return new Promise((resolve) => {
      let timer=setTimeout(()=>{
        clearTimeout(timer)
        resolve()
      }, time)
    })
  },
}
